package com.thoughtworks.biblioteca;

import java.util.HashMap;
import java.io.*;

import com.thoughtworks.biblioteca.exceptions.IllegalSelectionNumberException;

/**
 * Command design pattern, this is the invoker. It invokes the methods of the
 * receiver and holds all the items.
 */
public class Menu {

    private final static String MENU_ITEMS_PATH = "resource/MenuItems.lib";

    private HashMap<Integer, MenuItem> menuItems;

    public Menu() {
        menuItems = new HashMap<Integer, MenuItem>();
        loadItemsFromFile(this.getClass().getResource("/").getPath()
                + MENU_ITEMS_PATH);
    }

    /**
     * load menu items from file
     * @param MenuItemsFilePath
     * @return boolean
     */
    private boolean loadItemsFromFile(String MenuItemsFilePath) {

        File itemsFile = new File(MenuItemsFilePath);
        BufferedReader reader = null;
        if (itemsFile.exists()) {
            try {
                reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(itemsFile)));
                String itemInfo;
                String[] itemInfoArray;
                while ((itemInfo = reader.readLine()) != null
                        && !"".equals(itemInfo)) {
                    itemInfoArray = itemInfo.split(",");
                    menuItems.put(Integer.parseInt(itemInfoArray[0].trim()),
                            MenuItemFactory.getMenuItemInstance(
                                    itemInfoArray[1].trim(), itemInfoArray[2]
                                    .trim()));
                }
            } catch (FileNotFoundException e) {
                System.out
                        .println("Don't Find The Movie Library File: Movies.lib.");
                return false;
            } catch (IOException e) {
                System.out.println("Something wrong with the IO.");
                return false;
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    System.out.println("Something wrong with the IO.");
                    return false;
                }
            }
            return true;
        } else {
            System.out
                    .println("Don't Find The Movie Library File: Movies.lib.");
            return false;
        }

    }

    /**
     * get all the items of menu
     * @return HashMap
     */
    public HashMap<Integer, MenuItem> getMenuItems() {
        return menuItems;
    }

    /**
     * call the reciver to perform the command
     * @param itemNumber
     * @return boolean
     * @throws IllegalSelectionNumberException
     */
    public boolean executeAfterSelectionItem(Integer itemNumber)
            throws IllegalSelectionNumberException {
        if (menuItems.containsKey(itemNumber)) {
            menuItems.get(itemNumber).execute();
            return true;
        } else {
            throw new IllegalSelectionNumberException(
                    "\nEnter a valid integer!!");
        }
    }

    /**
     * get a specific MenuItem by its itemNumber
     * @param itemNumber
     * @return MenuItem
     */
    public MenuItem getMenuItemAt(Integer itemNumber) {
        return menuItems.get(itemNumber);
    }
}
